package com.cn.medicalcare.controller.backstageservlet.admin;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Admin;
import com.cn.medicalcare.service.AdminService;
import com.cn.medicalcare.service.serviceimpl.AdminServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/updatepassword.json")
public class UpdatePasswordServlet extends HttpServlet {

    private AdminService adminService = AdminServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String password = req.getParameter("password");
        String token = req.getParameter("sessionId");
        Map<String, Object> map = new LinkedHashMap<>();
        String adminJson = JedisUtil.jedisDo(j -> {
            j.select(15);
            return j.get(token);
        });
        Admin admin = JsonTool.toObject(adminJson, Admin.class);
        if (admin != null && password != null && password.length() > 4) {
            admin.setPassword(password);
            if (adminService.updataAdminPassword(admin)) {
                //修改成功
                map.put("status", 200);
            } else {
                map.put("status", 500);
            }
        } else {
            map.put("status", 400);
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }
}
